<?php

namespace App\Helpers\Api;


use Illuminate\Http\Request;

trait EditStatusTrait
{

    public function editStatus(Request $request) {
        $this->validate($request, [
            'ids' => 'required',
            'value' => 'required',
            'action' => 'required'
        ]);
        $value = \request('value');
        $action = \request('action');
        $reason = \request('reason', '');
        $ids = $request->input('ids');
        $arr_id = explode(",", $ids);

        $list = $this->model::whereIn('id', $arr_id)->get();
        foreach ($list as $item) {
            if ($action == 'audit') {
                $item->audit_status = $value;
                $item->reason = $reason;
            } elseif ($action == 'black') {
                $item->is_black = $value;
                $item->reason = $reason;
            } elseif ($action == 'vip') {
                $item->is_vip = $value;
            } else if($action == 'sort') {
                $item->sort = $value;
            } else if($action == 'on_status') {
                $item->on_status = $value;
            }
            if ($item->save()) {
                if (method_exists($this, '_after_editStatus')) {
                    $this->_after_editStatus($item, $action, $value, $reason);
                }
            }
        }
        return $this->success([]);
    }
}
